package com.gavindon.android.viewmodel

import androidx.lifecycle.viewModelScope
import com.gavindon.android.base.BaseViewModel
import com.gavindon.android.entity.CheckCanLookResp
import com.gavindon.android.entity.ComboResp
import com.gavindon.android.entity.StudyRecordResp
import com.gavindon.android.network.DataResult
import com.gavindon.android.repo.CommonRepo
import kotlinx.coroutines.launch

/**
 * description:
 * Created by liNan on  2022-2-11 09:26
 */
class CommonViewModel : BaseViewModel() {

    private val mRepo = CommonRepo()


    private lateinit var addLiveData: SingleLiveEvent<DataResult<String>>
    private lateinit var collectStatusLiveData: SingleLiveEvent<DataResult<Boolean>>
    private lateinit var cancelLiveData: SingleLiveEvent<DataResult<String>>
    private lateinit var payLookLiveData: SingleLiveEvent<DataResult<List<CheckCanLookResp>>>
    private val studyLiveData = SingleLiveEvent<DataResult<StudyRecordResp>>()

    //套餐
    private val comboLiveData = SingleLiveEvent<DataResult<List<ComboResp>>>()

    fun clientCollectStatus(id: String): SingleLiveEvent<DataResult<Boolean>> {
        collectStatusLiveData = SingleLiveEvent()
        viewModelScope.launch {
            val resp = mRepo.requestCollectStatus(id)
            collectStatusLiveData.postValue(resp)
        }
        return collectStatusLiveData
    }

    fun clientCollectAdd(id: String): SingleLiveEvent<DataResult<String>> {
        addLiveData = SingleLiveEvent()
        viewModelScope.launch {
            val resp = mRepo.requestCollectAdd(id)
            addLiveData.postValue(resp)
        }
        return addLiveData
    }

    fun clientCollectCancel(id: String): SingleLiveEvent<DataResult<String>> {
        cancelLiveData = SingleLiveEvent()
        viewModelScope.launch {
            val resp = mRepo.requestCollectCancel(id)
            cancelLiveData.postValue(resp)
        }
        return cancelLiveData
    }

    /*观看视频进度*/
    fun pushVideoProgress(id: String, second: Int) {
        viewModelScope.launch {
            mRepo.pushLessonProgress(id, second)
        }
    }

    /*查询套餐根据课程id*/
    fun queryCombLst(courseId: String): SingleLiveEvent<DataResult<List<ComboResp>>> {
        viewModelScope.launch {
            val resp = mRepo.queryCombLst(courseId)
            comboLiveData.value = resp
        }
        return comboLiveData
    }


    /**
     * 查询是否可以观看
     */
    fun checkIsCanLook(
        orderType: String,
        packageOrCourseId: String
    ): SingleLiveEvent<DataResult<List<CheckCanLookResp>>> {
        payLookLiveData = SingleLiveEvent()
        viewModelScope.launch {
            val resp = mRepo.payFinish(orderType, packageOrCourseId)
            payLookLiveData.value = resp
        }
        return payLookLiveData
    }

    /*获取收藏记录*/
    fun getStudyRecord(lessonId:String): SingleLiveEvent<DataResult<StudyRecordResp>> {
        viewModelScope.launch {
            val result = mRepo.getStudyRecord(lessonId)
            studyLiveData.value = result
        }
        return studyLiveData
    }
}